<?php

namespace App\Http\Controllers\Api;


use App\Model\Project;
use App\Model\Company;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use PhpOffice\PhpWord\TemplateProcessor;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\RequestStack;

class ProjectController extends Controller
{
    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request){
        $query  = Project::where('id','!=','');
        $data = $request->input();
        if (!empty($data['keywords'])){
            $query->where('project_name','like','%'.$data['keywords'].'%');
        }
        $data = $query->orderBy('id','desc')->get();
        $res_arr['data'] = $data;
        $res_arr["status"] = 200;
        $res_arr["message"] = 'success';
        return response()->json($res_arr);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function one(Request $request){
        $data = $request->input();
        if (empty($data['name'])){
            $res_arr['data'] = [];
            $res_arr["status"] = 403;
            $res_arr["message"] = '项目名称不能为空';
            return response()->json($res_arr);
        }
        $one = Project::where('project_name','=',$data['name'])->get();
        $one = json_decode(json_encode($one), true);
        if (!empty($one)){
            $res_arr['data'] = [];
            $res_arr["status"] = 405;
            $res_arr["message"] = '请勿输入重复的项目名称';
            return response()->json($res_arr);
        }
        $project = new Project();
        $project->project_name = $data['name'];
        $project->save();
        $res_arr['data'] = [];
        $res_arr["status"] = 200;
        $res_arr["message"] = 'success';
        return response()->json($res_arr);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(Request $request){
        $data = $request->input();
        if (empty($data['name'])){
            $res_arr['data'] = [];
            $res_arr["status"] = 403;
            $res_arr["message"] = '项目名称不能为空';
            return response()->json($res_arr);
        }
        $one = Project::where('project_name','=',$data['name'])->get();
        $one = json_decode(json_encode($one), true);
        if (!empty($one)){
            $res_arr['data'] = [];
            $res_arr["status"] = 405;
            $res_arr["message"] = '请勿输入重复的项目名称';
            return response()->json($res_arr);
        }
        Project::where('id','=',$data['project_id'])->update(['project_name'=>$data['name']]);
        $res_arr['data'] = [];
        $res_arr["status"] = 200;
        $res_arr["message"] = 'success';
        return response()->json($res_arr);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete(Request $request)
    {
        $data = $request->input();
        $id = $data['id'];
        $result  = DB::delete('delete from project where id = ?',[$id]);
        $res_arr['data'] = [];
        $res_arr["status"] = 200;
        $res_arr["message"] = 'success';
        return response()->json($res_arr);
    }

    /*
     *参数:projectId 项目id
     *获取项目下的公司列表
     */
    public function company(Request $request)
    {
        $projectId = $request->input('project_id');
        if(empty($projectId)) {
            $res_arr['data'] = [];
            $res_arr["status"] = 405;
            $res_arr["message"] = '参数错误';
            return response()->json($res_arr);
        }
        $where = array();
        $where[] = array('project', $projectId);
        $field = array("id", "comName", "schedule", "project");
        $companies = Company::where($where)->get($field);

        $res_arr['data'] = $companies;
        $res_arr["status"] = 200;
        $res_arr["message"] = 'success';
        return response()->json($res_arr);
    }

}
